package com.xyazm.market.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.market.exception.MarketBizException;
import com.xyazm.market.service.IMarketService;
import com.xyazm.market.domain.dto.CalculateOrderAmountDTO;
import com.xyazm.market.domain.param.CalculateOrderAmountParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 营销服务对外暴露的接口实现类
 * @author xuyou
 * 2022/6/1 0001 21:22
*/
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IMarketApi.class)
public class MarketApi implements IMarketApi {

    @Autowired
    private IMarketService marketService;

    @Override
    public JsonResult<CalculateOrderAmountDTO> calculateOrderAmount(CalculateOrderAmountParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            CalculateOrderAmountDTO calculateOrderAmountDTO = marketService.calculateOrderAmount(param);
            return JsonResult.buildSuccess(calculateOrderAmountDTO);
        } catch (MarketBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
